namespace QMVnSoft.Lists
{
    using QMVnSoft;
    using QMVnSoft.Controls;
    using QMVnSoft.Controls.ChildGridControls;
    using QMVnSoft.DAL;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class FormDmKCGTCTViewer : FormControl
    {
        private BtnControl btnCancel;
        private BtnControl btnGetValue;
        private BtnControl btnOk;
        private DataGridViewTextBoxColumn Column1;
        private DataGridViewTextBoxColumn Column2;
        private NumericColumn Column3;
        private NumericColumn Column4;
        private NumericColumn Column5;
        private IContainer components;
        private GridDetail grdDetail;
        private LabelControl labelControl2;
        private LabelControl lblTTien_Nt0;
        private LabelControl lblTTien_PB;
        private DataTable LocalDetailTable;
        private DmPBCTDataAccess oMethod;
        private object oUserDefined1;
        private object oUserDefined2;
        private object oUserDefined3;
        private object oUserDefined4;
        private TextNumeric txtTTien0;
        private TextNumeric txtTTien1;

        public FormDmKCGTCTViewer()
        {
            this.oMethod = new DmPBCTDataAccess();
            this.InitializeComponent();
        }

        public FormDmKCGTCTViewer(object poUserDefined1, object poUserDefined2, object poUserDefined3, object poUserDefined4)
        {
            this.oMethod = new DmPBCTDataAccess();
            this.InitializeComponent();
            this.UserDefined1 = poUserDefined1;
            this.UserDefined2 = poUserDefined2;
            this.UserDefined3 = poUserDefined3;
            this.UserDefined4 = poUserDefined4;
            this.FillData();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            base.Close();
        }

        private void btnGetValue_Click(object sender, EventArgs e)
        {
            if ((((DateTime) this.UserDefined2) < AppInstance.OpeningDateOfSubEnterprice) || (((DateTime) this.UserDefined2) <= AppInstance.EndingDateOfSubEnterprice))
            {
                MessageBox.Show("Đã khóa dữ liệu!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.GetNewData();
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if ((((DateTime) this.UserDefined2) < AppInstance.OpeningDateOfSubEnterprice) || (((DateTime) this.UserDefined2) <= AppInstance.EndingDateOfSubEnterprice))
            {
                MessageBox.Show("Đã khóa dữ liệu!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.OnSave();
                base.Close();
            }
        }

        private DataTable CreateInvoice()
        {
            DataTable table = new DataTable();
            DataColumn column = new DataColumn("Stt0_No", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Stt0_Co", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Tk_No", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Tk_Co", typeof(string));
            table.Columns.Add(column);
            column = new DataColumn("Tien", typeof(decimal));
            table.Columns.Add(column);
            column = new DataColumn("Ma_Sp", typeof(string));
            table.Columns.Add(column);
            foreach (DataRow row2 in this.LocalDetailTable.Rows)
            {
                if (!(((decimal) row2["Tien1"]) == 0M))
                {
                    DataRow row = table.NewRow();
                    row["Stt0_No"] = AppDataAccess.Cong_So_Tt(AppInstance.Subsidiary);
                    row["Stt0_Co"] = AppDataAccess.Cong_So_Tt(AppInstance.Subsidiary);
                    row["Tk_No"] = ((DataRow) this.UserDefined4)["Tk_Den"];
                    row["Tk_Co"] = ((DataRow) this.UserDefined4)["Tk_Di"];
                    row["Ma_Sp"] = row2["Ma_Sp"];
                    row["Tien"] = row2["Tien1"];
                    table.Rows.Add(row);
                }
            }
            return table;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void FillData()
        {
            object obj2 = 0;
            object obj3 = 0;
            this.LocalDetailTable = this.oMethod.GetTablePB(AppInstance.Subsidiary, this.UserDefined1, this.UserDefined2, ((DataRow) this.UserDefined4)["Stt"], ref obj2, ref obj3);
            this.grdDetail.DataSource = new BindingSource(this.LocalDetailTable, this.LocalDetailTable.TableName);
            this.txtTTien0.Value = obj2;
            this.txtTTien1.Value = obj3;
        }

        private void GetNewData()
        {
            object obj2 = 0;
            object obj3 = 0;
            this.LocalDetailTable = this.oMethod.PhanBo_GTCT(AppInstance.Subsidiary, this.UserDefined1, this.UserDefined2, ((DataRow) this.UserDefined4)["Stt"], ref obj2, ref obj3);
            this.grdDetail.DataSource = new BindingSource(this.LocalDetailTable, this.LocalDetailTable.TableName);
            this.txtTTien0.Value = obj2;
            this.txtTTien1.Value = obj3;
        }

        private void grdDetail_CellBottomLeftLeaved(object sender, EventArgs e)
        {
            this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.NextCell;
        }

        private void grdDetail_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex >= 3)
            {
                DataRow row = ((DataRowView) this.grdDetail.CurrentRow.DataBoundItem).Row;
                if (e.ColumnIndex == 3)
                {
                    row["Tien1"] = decimal.Round((((decimal) row["Tien0"]) * ((decimal) row["He_So"])) / 100M, AppInstance.LCAmountDecimals);
                }
                else if (e.ColumnIndex == 4)
                {
                    row["He_So"] = decimal.Round((((decimal) row["Tien1"]) / ((decimal) row["Tien0"])) * 100M, AppInstance.PercentDecimals);
                }
                this.Sum();
            }
        }

        private void grdDetail_CellRightLeaved(object sender, EventArgs e)
        {
            if ((this.grdDetail.Rows.Count - 1) == this.grdDetail.CurrentRow.Index)
            {
                this.btnOk.Focus();
            }
            else
            {
                this.grdDetail.UserMoveNextCell = GridDetail.MoveNextType.FirstCellNextRow;
            }
        }

        private void grdDetail_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.ColumnIndex >= 3)
            {
                DataRow row = ((DataRowView) this.grdDetail.CurrentRow.DataBoundItem).Row;
                if (e.ColumnIndex == 3)
                {
                    if (this.grdDetail.CurrentCell.IsInEditMode)
                    {
                        row["He_So"] = Convert.ToDecimal(string.IsNullOrEmpty(e.FormattedValue.ToString()) ? "0" : e.FormattedValue);
                    }
                    if ((((decimal) row["He_So"]) > 100M) || (((decimal) row["He_So"]) < 0M))
                    {
                        MessageBox.Show("Hệ số phân bổ phải từ 0% đến 100%!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        e.Cancel = true;
                        (this.grdDetail.CurrentCell as NumericCell).CheckMovable = false;
                    }
                    else
                    {
                        e.Cancel = false;
                        (this.grdDetail.CurrentCell as NumericCell).CheckMovable = true;
                    }
                }
                else if (e.ColumnIndex == 4)
                {
                    if (this.grdDetail.CurrentCell.IsInEditMode)
                    {
                        row["Tien1"] = Convert.ToDecimal(string.IsNullOrEmpty(e.FormattedValue.ToString()) ? "0" : e.FormattedValue);
                    }
                    if ((((decimal) row["Tien1"]) > ((decimal) row["Tien0"])) || (((decimal) row["Tien1"]) < 0M))
                    {
                        MessageBox.Show("Giá thành công trình phải từ 0 đến " + ((decimal) row["Tien0"]).ToString("N" + AppInstance.LCAmountDecimals.ToString()) + "!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        e.Cancel = true;
                        (this.grdDetail.CurrentCell as NumericCell).CheckMovable = false;
                    }
                    else
                    {
                        e.Cancel = false;
                        (this.grdDetail.CurrentCell as NumericCell).CheckMovable = true;
                    }
                }
            }
        }

        private void InitializeComponent()
        {
            //ComponentResourceManager manager = new ComponentResourceManager(typeof(FormDmKCGTCTViewer));
            DataGridViewCellStyle style = new DataGridViewCellStyle();
            DataGridViewCellStyle style2 = new DataGridViewCellStyle();
            DataGridViewCellStyle style3 = new DataGridViewCellStyle();
            this.txtTTien1 = new TextNumeric();
            this.lblTTien_PB = new LabelControl();
            this.txtTTien0 = new TextNumeric();
            this.labelControl2 = new LabelControl();
            this.lblTTien_Nt0 = new LabelControl();
            this.btnOk = new BtnControl();
            this.btnCancel = new BtnControl();
            this.grdDetail = new GridDetail();
            this.Column1 = new DataGridViewTextBoxColumn();
            this.Column2 = new DataGridViewTextBoxColumn();
            this.Column5 = new NumericColumn();
            this.Column3 = new NumericColumn();
            this.Column4 = new NumericColumn();
            this.btnGetValue = new BtnControl();
            ((ISupportInitialize) this.grdDetail).BeginInit();
            base.SuspendLayout();
            this.txtTTien1.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.txtTTien1.BackColor = Color.White;
            this.txtTTien1.Enabled = false;
            this.txtTTien1.Font = new Font("Arial", 8.25f);
            this.txtTTien1.ForeColor = Color.Black;
            this.txtTTien1.Location = new Point(0x298, 0x1c9);
            this.txtTTien1.Name = "txtTTien1";
            int[] bits = new int[4];
            this.txtTTien1.NumericValue = new decimal(bits);
            this.txtTTien1.OldValue = null;
            this.txtTTien1.Size = new Size(120, 20);
            this.txtTTien1.TabIndex = 0x6a;
            this.txtTTien1.TextAlign = HorizontalAlignment.Right;
            this.txtTTien1.UserTextNumericFormat = AppInstance.TextNumericFormat.ForeignAmount;
//            this.txtTTien1.Value = null;//manager.GetObject("txtTTien1.Value");
            this.lblTTien_PB.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.lblTTien_PB.AutoSize = true;
            this.lblTTien_PB.Font = new Font("Arial", 8.25f);
            this.lblTTien_PB.Location = new Point(580, 460);
            this.lblTTien_PB.Name = "lblTTien_PB";
            this.lblTTien_PB.Size = new Size(0x4e, 14);
            this.lblTTien_PB.TabIndex = 0x6d;
            this.lblTTien_PB.Text = "Tổng giá thành";
            this.txtTTien0.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.txtTTien0.BackColor = Color.White;
            this.txtTTien0.Enabled = false;
            this.txtTTien0.Font = new Font("Arial", 8.25f);
            this.txtTTien0.ForeColor = Color.Black;
            this.txtTTien0.Location = new Point(0x5c, 0x1c9);
            this.txtTTien0.Name = "txtTTien0";
            int[] numArray2 = new int[4];
            this.txtTTien0.NumericValue = new decimal(numArray2);
            this.txtTTien0.OldValue = null;
            this.txtTTien0.Size = new Size(120, 20);
            this.txtTTien0.TabIndex = 0x69;
            this.txtTTien0.TextAlign = HorizontalAlignment.Right;
            this.txtTTien0.UserTextNumericFormat = AppInstance.TextNumericFormat.ForeignAmount;
//            this.txtTTien0.Value = null;//manager.GetObject("txtTTien0.Value");
            this.labelControl2.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.labelControl2.BorderStyle = BorderStyle.Fixed3D;
            this.labelControl2.Font = new Font("Arial", 8.25f);
            this.labelControl2.Location = new Point(0, 0x1e5);
            this.labelControl2.Name = "labelControl2";
            this.labelControl2.Size = new Size(0x31c, 2);
            this.labelControl2.TabIndex = 110;
            this.lblTTien_Nt0.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
            this.lblTTien_Nt0.AutoSize = true;
            this.lblTTien_Nt0.Font = new Font("Arial", 8.25f);
            this.lblTTien_Nt0.Location = new Point(9, 460);
            this.lblTTien_Nt0.Name = "lblTTien_Nt0";
            this.lblTTien_Nt0.Size = new Size(0x41, 14);
            this.lblTTien_Nt0.TabIndex = 0x6f;
            this.lblTTien_Nt0.Text = "Tổng chi phí";
            this.btnOk.Font = new Font("Arial", 8.25f);
            this.btnOk.Location = new Point(0x20c, 0x1f3);
            this.btnOk.Name = "btnOk";
            this.btnOk.Size = new Size(0x7f, 0x17);
            this.btnOk.TabIndex = 0x6b;
            this.btnOk.Text = "&Chấp nhận";
            this.btnOk.UseVisualStyleBackColor = true;
            this.btnOk.Click += new EventHandler(this.btnOk_Click);
            //this.btnCancel.DialogResult = DialogResult.Cancel;
            this.btnCancel.Font = new Font("Arial", 8.25f);
            this.btnCancel.Location = new Point(0x291, 0x1f3);
            this.btnCancel.Name = "btnCancel";
            this.btnCancel.Size = new Size(0x7f, 0x17);
            this.btnCancel.TabIndex = 0x6c;
            this.btnCancel.Text = "&Huỷ bỏ";
            this.btnCancel.UseVisualStyleBackColor = true;
            this.btnCancel.Click += new EventHandler(this.btnCancel_Click);
            this.grdDetail.AllowUserToAddRows = false;
            this.grdDetail.AllowUserToDeleteRows = false;
            this.grdDetail.CheckColor = false;
            this.grdDetail.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.grdDetail.Columns.AddRange(new DataGridViewColumn[] { this.Column1, this.Column2, this.Column5, this.Column3, this.Column4 });
            this.grdDetail.Font = new Font("Arial", 8.25f);
            this.grdDetail.Location = new Point(0, 0);
            this.grdDetail.Name = "grdDetail";
            this.grdDetail.Size = new Size(0x31a, 0x1c3);
            this.grdDetail.TabIndex = 0x68;
            this.grdDetail.CellRightLeaved += new EventHandler(this.grdDetail_CellRightLeaved);
            this.grdDetail.CellBottomLeftLeaved += new EventHandler(this.grdDetail_CellBottomLeftLeaved);
            this.grdDetail.CellValidating += new DataGridViewCellValidatingEventHandler(this.grdDetail_CellValidating);
            this.grdDetail.CellEndEdit += new DataGridViewCellEventHandler(this.grdDetail_CellEndEdit);
            this.Column1.DataPropertyName = "Ma_Sp";
            this.Column1.HeaderText = "Công trình";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Resizable = DataGridViewTriState.True;
            this.Column2.DataPropertyName = "Ten_Sp";
            this.Column2.HeaderText = "Tên công trình";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Width = 0x141;
            this.Column5.DataPropertyName = "Tien0";
            style.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column5.DefaultCellStyle = style;
            this.Column5.HeaderText = "Chi phí";
            this.Column5.Name = "Column5";
            this.Column5.ReadOnly = true;
            this.Column5.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column5.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalAmount;
            this.Column5.Width = 120;
            this.Column3.DataPropertyName = "He_So";
            style2.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column3.DefaultCellStyle = style2;
            this.Column3.HeaderText = "Hệ số";
            this.Column3.Name = "Column3";
            this.Column3.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column3.UserTextNumericFormat = AppInstance.TextNumericFormat.Percent;
            this.Column3.Width = 90;
            this.Column4.DataPropertyName = "Tien1";
            style3.Alignment = DataGridViewContentAlignment.MiddleRight;
            this.Column4.DefaultCellStyle = style3;
            this.Column4.HeaderText = "Giá thành";
            this.Column4.Name = "Column4";
            this.Column4.SortMode = DataGridViewColumnSortMode.Automatic;
            this.Column4.UserTextNumericFormat = AppInstance.TextNumericFormat.LocalAmount;
            this.Column4.Width = 120;
            this.btnGetValue.Font = new Font("Arial", 8.25f);
            this.btnGetValue.Location = new Point(12, 0x1f3);
            this.btnGetValue.Name = "btnGetValue";
            this.btnGetValue.Size = new Size(0x7f, 0x17);
            this.btnGetValue.TabIndex = 0x6b;
            this.btnGetValue.Text = "&X\x00e1c định chi phí";
            this.btnGetValue.UseVisualStyleBackColor = true;
            this.btnGetValue.Click += new EventHandler(this.btnGetValue_Click);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.CancelButton = this.btnCancel;
            base.ClientSize = new Size(0x31c, 0x216);
            base.Controls.Add(this.txtTTien1);
            base.Controls.Add(this.lblTTien_PB);
            base.Controls.Add(this.txtTTien0);
            base.Controls.Add(this.labelControl2);
            base.Controls.Add(this.lblTTien_Nt0);
            base.Controls.Add(this.btnGetValue);
            base.Controls.Add(this.btnOk);
            base.Controls.Add(this.btnCancel);
            base.Controls.Add(this.grdDetail);
            //base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            base.MinimizeBox = false;
            this.MinimumSize = new Size(0x322, 0x236);
            base.Name = "FormDmKCGTCTViewer";
            this.Text = "Kết chuyển giá thành công trình";
            ((ISupportInitialize) this.grdDetail).EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void OnSave()
        {
            if ((this.LocalDetailTable == null) || (this.LocalDetailTable.Rows.Count == 0))
            {
                MessageBox.Show("Chưa có giá trị phân bổ!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
            else
            {
                this.oMethod.SaveTablePB(AppInstance.Subsidiary, this.UserDefined1, this.UserDefined2, ((DataRow) this.UserDefined4)["Stt"], this.LocalDetailTable);
                this.oMethod.CreateInvoice(AppInstance.Subsidiary, this.UserDefined2, ((DataRow) this.UserDefined4)["Dien_Giai"], ((DataRow) this.UserDefined4)["Desc_EN"], AppInstance.DefaultCurrency, AppInstance.UserName, AppInstance.PostDefault ? 1 : 0, AppInstance.LockDefault, this.CreateInvoice());
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.Control | Keys.Return))
            {
                if ((((DateTime) this.UserDefined2) < AppInstance.OpeningDateOfSubEnterprice) || (((DateTime) this.UserDefined2) <= AppInstance.EndingDateOfSubEnterprice))
                {
                    MessageBox.Show("Đã khóa dữ liệu!", AppInstance.SoftwareName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return false;
                }
                this.OnSave();
                base.Close();
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        private void Sum()
        {
            decimal num = 0M;
            decimal num2 = 0M;
            foreach (DataRow row in this.LocalDetailTable.Rows)
            {
                num += (decimal) row["Tien0"];
                num2 += (decimal) row["Tien1"];
            }
            this.txtTTien0.Value = num;
            this.txtTTien1.Value = num2;
        }

        public object UserDefined1
        {
            get
            {
                return this.oUserDefined1;
            }
            set
            {
                this.oUserDefined1 = value;
            }
        }

        public object UserDefined2
        {
            get
            {
                return this.oUserDefined2;
            }
            set
            {
                this.oUserDefined2 = value;
            }
        }

        public object UserDefined3
        {
            get
            {
                return this.oUserDefined3;
            }
            set
            {
                this.oUserDefined3 = value;
            }
        }

        public object UserDefined4
        {
            get
            {
                return this.oUserDefined4;
            }
            set
            {
                this.oUserDefined4 = value;
            }
        }
    }
}

